package Register

import (
	"MedicalTreatment/global"
	model "MedicalTreatment/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
)

type Array struct {
	Start_time  string
	End_time    string
	Iseffective bool
}

// 挂号查询接口
func RegSel(r *gin.Engine) {
	r.POST("/regSel", func(context *gin.Context) {
		var gh model.RegisterSel
		if err := context.ShouldBindJSON(&gh); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}

		var times []Array
		layout := "15:04:05"
		start := "08:00:00"
		end := "17:00:00"
		start_wu := "10:50:00"
		end_wu := "14:00:00"
		startTimeSleep, err := time.Parse(layout, start_wu)

		if err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})

			return
		}
		endTimeSleep, err := time.Parse(layout, end_wu)
		if err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		startTime, err := time.Parse(layout, start)
		if err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		endTime, err := time.Parse(layout, end)
		if err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}

		for t := startTime; t.Before(endTime); t = t.Add(10 * time.Minute) {
			var a Array
			a.Start_time = t.Format("15:04:05")
			a.End_time = t.Add(10 * time.Minute).Format("15:04:05")
			a.Iseffective = true
			//current := t.Format("15:04:05")
			if t.After(startTimeSleep) && t.Before(endTimeSleep) {
				continue
			}

			times = append(times, a)
		}
		fmt.Println(times)
		var reg []model.Reg
		if err := global.DB.Model(&model.Register{}).Where("doc_id = ? AND date Like ?", gh.Doc_id, gh.Date+"%").Find(&reg).Error; err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		fmt.Println(reg)
		for i := 0; i < len(times); i++ {
			for j := 0; j < len(reg); j++ {
				if times[i].Start_time == reg[j].StartTime {
					times[i].Iseffective = false
				}
			}

		}
		context.JSON(200, gin.H{
			"Msg": times,
		})
	})
}
